<template>
  <div class="box">
    <el-card>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="用户管理" name="first">登录账户设置</el-tab-pane>
        <el-tab-pane label="配置管理" name="second">
          <!-- 个人信息 -->
          <el-form ref="userForm" :model="userInfo" :rules="userRules" label-width="220px">
            <!--手机 -->

            <el-form-item label="手机" prop="mobile">
              <el-input v-model="userInfo.mobile" style="width:220px" />
            </el-form-item>

            <!-- 工号 入职时间 -->

            <el-form-item label="入职时间" prop="timeOfEntry">
              <el-date-picker
                v-model="userInfo.timeOfEntry"
                type="date"
                class="inputW"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>

            <!-- 员工照片 -->

            <el-form-item label="员工头像">
              <!-- 放置上传图片 -->
              <PayloadImg ref="PayloadImg" />
            </el-form-item>

            <!-- 保存个人信息 -->
            <el-form-item>
              <el-button type="primary" @click="save">保存更新</el-button>
              <el-button @click="$router.back()">返回</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="配置管理" name="third">岗位信息</el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/user'
import { updateUserList } from '@/api/employees'

export default {
  name: 'VueAdminTemplateMasterDetails',

  data() {
    return {
      activeName: 'second',
      userId: this.$route.params.id,
      userInfo: {
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      userRules: {
        mobile: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '必填项', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    this.getUserInfo()
  },

  methods: {
    // 根据id获取数据
    async getUserInfo() {
      const res = await getUserInfo(this.userId)
      if (res.code === 10000) {
        this.userInfo = res.data
        this.$refs.PayloadImg.imageUrl = res.data.staffPhoto
      }
    },

    // 保存
    save() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          const res = await updateUserList({ ...this.userInfo, staffPhoto: this.$refs.PayloadImg.imageUrl })
          if (res.code === 10000) {
            if (this.userId === this.$store.state.user.userInfo.id) {
              this.$store.dispatch('user/getInfo')
            }
          }
        }
      })
    },

    handleClick(tab, event) {
      console.log(tab, event)
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  padding: 30px;
}
</style>
